package com.sentinel.demo.basic;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.sentinel.demo.basic.common.InitRuleUtils;
import com.sentinel.demo.basic.controller.BasicController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@ComponentScan("com.sentinel.demo.*")//启动时，如果需要spring初始化其他引入的自定义的bean，那么相关类包路径需要设置
public class SentinelDemoBasicApplication {

	public static void main(String[] args) {
		SpringApplication.run(SentinelDemoBasicApplication.class, args);
		//初始化规则
		InitRuleUtils.initFlowQpsRule(BasicController.KEY);
		//初始化降级规则
		InitRuleUtils.initDegradeRule("listBrand3" ,RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT ,3 ,10 ,RuleConstant.DEGRADE_DEFAULT_MIN_REQUEST_AMOUNT);
	}
	private static void initFlowQpsRule() {
		List<FlowRule> rules = new ArrayList<FlowRule>();
		FlowRule rule1 = new FlowRule();
		rule1.setResource(BasicController.KEY);
		// set limit qps to 1
		rule1.setCount(1);
		rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
		rule1.setLimitApp("default");
		rules.add(rule1);
		FlowRuleManager.loadRules(rules);
	}
}
